package com.yogee.topseller.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.yogee.core.http.listener.HttpOnNextListener;
import com.yogee.core.http.subscribers.BaseSubscriber;
import com.yogee.core.utils.ToastUtils;
import com.yogee.topseller.R;
import com.yogee.topseller.activity.ProductDetailsActivity;
import com.yogee.topseller.bean.MySelfInfo;
import com.yogee.topseller.bean.ProductInfoBean;
import com.yogee.topseller.bean.ResultBean;
import com.yogee.topseller.http.HttpManager;
import com.yogee.topseller.utils.AppUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * @auathor ${XiaoJianghong}
 * @date 2017/6/28.
 */

public class ProductInterestedRvAdapter extends RecyclerView.Adapter<ProductInterestedRvAdapter.ItemHolder> {

    private Context context;
    private List<ProductInfoBean.InterestBean> beans = new ArrayList<>();

    public ProductInterestedRvAdapter(Context context) {
        this.context = context;
    }

    public void addData(List<ProductInfoBean.InterestBean> beans) {
        this.beans = beans;
        notifyDataSetChanged();
    }

    @Override
    public ItemHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_product_interest_list, parent, false);
        return new ItemHolder(view);
    }

    @Override
    public void onBindViewHolder(final ItemHolder holder, int position) {
        final ProductInfoBean.InterestBean bean = beans.get(position);

        holder.itemProductNameTv.setText(bean.getName());
        String labels = "";

        holder.itemProductDetailsTv.setText(bean.getProType());
        holder.itemProductQtMoneyTv.setText(bean.getStartAmount());
        holder.itemProductSyTv.setText(bean.getExpectReturn().substring(0,bean.getExpectReturn().length()-1));
        holder.itemProductMonthTv.setText(bean.getInvestmentTerm());
        if(!AppUtil.isExamine(context)){
            holder.itemProductYongBiTv.setText("请登录");
            holder.itemProductYongProTv.setText("请登录");
            holder.itemProductYongBiTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
            holder.itemProductYongProTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
            holder.itemProductYongPb.setProgress(0);
            }else{
                holder.itemProductYongBiTv.setText(bean.getCommission());
                if(MySelfInfo.getInstance().getIsPay().equals("0")){
                    holder.itemProductYongProTv.setText(bean.getPercent() + "%");
                    holder.itemProductYongPb.setProgress(Integer.valueOf(bean.getPercent()));
                    holder.itemProductYongProTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                    holder.itemProductYongBiTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
                }else{
                    holder.itemProductYongProTv.setText("会员可见");
                    holder.itemProductYongBiTv.setText("会员可见");
                    holder.itemProductYongProTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                    holder.itemProductYongPb.setProgress(0);
                    holder.itemProductYongBiTv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 10);
                }

        }

        holder.itemProductLl.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context, ProductDetailsActivity.class).putExtra("id", bean.getId()).putExtra("isFast", "0"));
            }
        });

        final TextView btn = holder.tv_item_compare_btn;
        final ImageView db = holder.item_product_plus_iv;
        if(bean.getIsCompared().equals("0")){
            btn.setText("加对比");
            btn.setTextColor(context.getResources().getColor(R.color.colorStyle));
            db.setImageResource(R.mipmap.product_plus_sign_red);
            holder.itemProductAddRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(AppUtil.isExamined(context)){
                        String userid = AppUtil.getUserId(context);
                        addCompare(bean.getId(), userid,btn,db);
                    }

                }
            });
        }else{

            btn.setText("已添加");
            btn.setTextColor(context.getResources().getColor(R.color.gray));
            db.setImageResource(R.mipmap.product_plus_sign_gray);
            holder.itemProductAddRl.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if(AppUtil.isExamined(context)){
                        String userid = AppUtil.getUserId(context);
                        addCompare(bean.getId(), userid,btn,db);
                    }

                }
            });
        }
        // 预热和图片
        if(bean.getImgstuats().equals("1")){
            holder.img_tu.setVisibility(View.VISIBLE);
        }else{
            holder.img_tu.setVisibility(View.GONE);
        }

        if(bean.getStatus().equals("预热中"))
            holder.img_yu.setVisibility(View.VISIBLE);
        else{
            holder.img_yu.setVisibility(View.GONE);
        }
        if(bean.getStatus().equals("售罄")){
            holder.img_sell_out.setVisibility(View.VISIBLE);
    }else
            holder.img_sell_out.setVisibility(View.GONE);

    }

    @Override
    public int getItemCount() {
        return beans == null ? 0 : beans.size();
    }

    private void addCompare(String proId, String userId,final TextView button,final ImageView db) {
        HttpManager.getInstance().addCompare(proId, userId).compose(((ProductDetailsActivity) context).bindRecycler()).subscribe(new BaseSubscriber(new HttpOnNextListener<ResultBean>() {
            @Override
            public void onNext(ResultBean o) {
                if (o.getResult().equals("1")) {
                    ToastUtils.showToast(context, "添加成功");
                    button.setText("已添加");
                    button.setTextColor(context.getResources().getColor(R.color.gray));
                    db.setImageResource(R.mipmap.product_plus_sign_gray);
                    ((ProductDetailsActivity) context).editNum();
                }
            }
        }, ((ProductDetailsActivity) context)));
    }

    class ItemHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.item_product_name_tv)
        TextView itemProductNameTv;
//        @BindView(R.id.item_product_mj_tv)
//        TextView itemProductMjTv;
        @BindView(R.id.item_product_details_tv)
        TextView itemProductDetailsTv;
        @BindView(R.id.item_product_qt_money_tv)
        TextView itemProductQtMoneyTv;
        @BindView(R.id.item_product_sy_tv)
        TextView itemProductSyTv;
        @BindView(R.id.item_product_month_tv)
        TextView itemProductMonthTv;
        @BindView(R.id.item_product_yong_bi_tv)
        TextView itemProductYongBiTv;
        @BindView(R.id.item_product_yong_pro_tv)
        TextView itemProductYongProTv;
        @BindView(R.id.item_product_yong_pb)
        ProgressBar itemProductYongPb;
        @BindView(R.id.item_product_add_rl)
        RelativeLayout itemProductAddRl;
        @BindView(R.id.item_list_ll)
        LinearLayout itemProductLl;
        @BindView(R.id.img_yu)
        ImageView img_yu;
        @BindView(R.id.img_tu)
        ImageView img_tu;
        @BindView(R.id.img_sell_out)
        ImageView img_sell_out;
        @BindView(R.id.tv_item_compare_btn)
        TextView tv_item_compare_btn;
        @BindView(R.id.item_product_plus_iv)
        ImageView item_product_plus_iv;


        public ItemHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

}
